
package example

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
)

type HeroService struct {}
// CreateHero 创建英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService) CreateHero(HR *example.Hero) (err error) {
	err = global.GVA_DB.Create(HR).Error
	return err
}

// DeleteHero 删除英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService)DeleteHero(ID string) (err error) {
	err = global.GVA_DB.Delete(&example.Hero{},"id = ?",ID).Error
	return err
}

// DeleteHeroByIds 批量删除英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService)DeleteHeroByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]example.Hero{},"id in ?",IDs).Error
	return err
}

// UpdateHero 更新英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService)UpdateHero(HR example.Hero) (err error) {
	err = global.GVA_DB.Model(&example.Hero{}).Where("id = ?",HR.ID).Updates(&HR).Error
	return err
}

// GetHero 根据ID获取英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService)GetHero(ID string) (HR example.Hero, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&HR).Error
	return
}
// GetHeroInfoList 分页获取英雄信息记录
// Author [yourname](https://github.com/yourname)
func (HRService *HeroService)GetHeroInfoList(info exampleReq.HeroSearch) (list []example.Hero, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.Hero{})
    var HRs []example.Hero
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["hero_name"] = true
         	orderMap["health"] = true
         	orderMap["attack"] = true
         	orderMap["defense"] = true
         	orderMap["speed"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&HRs).Error
	return  HRs, total, err
}
func (HRService *HeroService)GetHeroPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
